/**
 * Created by Fly on 2017/6/29.
 */
(function (window, document) {
    var HttpClient = function () {
        var xmlhttp = {};
        /**
         * get请求
         * @param url url地址
         * @param data json对象
         * @param callback 成功回调
         * @param errcallback 错误回调
         */
        httpclient.ajaxGet = function (url, data, callback, errcallback) {
            xmlHttp = getXmlHttpObject();
            if (xmlHttp == null) {
                errcallback("您的浏览器不支持AJAX", -1);
                return;
            }
            var queryString = createQueryString(data);
            if (queryString.length > 0) {
                url = url + '?' + queryString;
            }
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange = checkStateChange(function (response, result, status) {
                if (result) {
                    callback(response);
                } else {
                    errcallback(response, status);
                }
            });//发送事件后，收到信息了调用函数
            xmlHttp.send();
        };
        /**
         * post请求
         * @param url url地址
         * @param data json对象
         * @param callback 成功回调
         * @param errcallback 错误回调
         * @param type 请求内容类型
         */
        httpclient.ajaxPost = function (url, data, callback, errcallback, type) {
            xmlhttp = getXmlHttpObject();
            if (xmlHttp == null) {
                errcallback("您的浏览器不支持AJAX", -1);
                return;
            }
            xmlhttp.open("POST", url, true);
            xmlhttp.setRequestHeader("Content-type", getContentType(type));
            xmlhttp.send(createQueryString(data));
            xmlHttp.onreadystatechange = checkStateChange(function (response, result, status) {
                if (result) {
                    callback(response);
                } else {
                    errcallback(response, status);
                }
            });//发送事件后，收到信息了调用函数
        };
        /**
         * 得到Post请求数据
         * @param data
         * @returns {string}
         */
        function createQueryString(data) {
            var queryString = '';
            if (typeof(data) == 'object') {
                for (var index in data) {
                    queryString += (data[index].Key + '=' + data[index].Value + '&');
                }
                if (queryString.length > 0) {
                    queryString = queryString.substring(0, queryString.length - 1);
                }
            }
            return encodeURI(encodeURI(queryString));//防止乱码
        }
        /**
         * 得到XmlHttp
         * @returns {{}}
         */
        function getXmlHttpObject() {
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            return xmlhttp;
        };
        /**
         * 检查状态更改
         * @param callback 发送数据后回调函数
         */
        function checkStateChange(callback) {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    callback(xmlhttp.responseText, true, xmlhttp.status);
                } else {
                    callback(xmlhttp.responseText, false, xmlhttp.status);
                }
            }
        }
        /**
         * 得到请求类型
         * @param type
         */
        function getContentType(type) {
            var defaultConentType = 'application/json; charset=utf-8';
            if (typeof(type) == 'string') {
                if (type.length > 0) {
                    return type;
                } else {
                    return defaultConentType;
                }
            }else{
                return defaultConentType;
            }
        }

        return xmlHttp;
    };
    window.httpclient = new HttpClient();
    httpclient = window.httpclient || new HttpClient();
})(window, document);